package com.ymt.bpm.service.platform.log.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.model.Const.Oplog;
import com.ymt.bpm.model.POperationlog;
import com.ymt.bpm.service.platform.log.OpLogService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.DateUtil;
import com.ymt.bpm.util.DhxUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Johnny on 2017/3/22.
 */
@Service
public class OpLogServiceImpl implements OpLogService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private String mapper = "POperationlogMapper";

    @Autowired
    private MDao mDao;

    @Override
    public int plog(String tenantId, String loginName, String displayName, String ip, Oplog.OP op, Oplog.CODE code, String desc) {
        POperationlog log = new POperationlog();
        if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
            log.setTenantId(Integer.parseInt(tenantId));
        }
        log.setCategory(POperationlog.CATEGORY_PROCESS);
        log.setOperator(loginName);
        log.setOperatorname(displayName);
        log.setIpaddress(ip);
        log.setOperation(op.name());
        log.setCode(code.name());
        log.setDescription(desc);
        return addLog(log);
    }

    @Override
    public int bizlog(String tenantId, String loginName, String displayName, String ip, Oplog.OP op, Oplog.CODE code, String desc) {
        POperationlog log = new POperationlog();
        if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
            log.setTenantId(Integer.parseInt(tenantId));
        }
        log.setCategory(POperationlog.CATEGORY_BUSINESS);
        log.setOperator(loginName);
        log.setOperatorname(displayName);
        log.setIpaddress(ip);
        log.setOperation(op.name());
        log.setCode(code.name());
        log.setDescription(desc);
        return addLog(log);
    }

    @Override
    public int addLog(POperationlog log) {
        if (log.getLogid()==null || log.getLogid().length()==0) {
            log.setLogid("LOG-"+ UUID.randomUUID().toString());
        }
        log.setLogdate(DateUtil.getGMTDate());
        return mDao.insert(mapper, "insert", log);
    }

    @Override
    public JSONObject get(String tenantId, String logid, String lang, int timezoneOffset) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(Const.TENANT_ID, tenantId);
        paramMap.put("logid", logid);
        POperationlog log = mDao.selectOne(mapper, "selectByPrimaryKey", paramMap);
        return DhxUtil.toFastJsonWithDate(log, lang, timezoneOffset);
    }

    @Override
    public int delLog(String tenantId, String logid) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(Const.TENANT_ID, tenantId);
        paramMap.put("logid", logid);
        return mDao.delete(mapper, "deleteByPrimaryKey", paramMap);
    }

    @Override
    public int purge(String tenantId) {
        return mDao.delete(mapper, "deleteAll", tenantId);
    }

    @Override
    public JSONObject query(String tenantId, String category,
                            String search, String sort, String order,
                            int offset, int limit, String lang, int timezoneOffset) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(Const.TENANT_ID, tenantId);
        paramMap.put("category", category);
        paramMap.put("search", search);
        paramMap.put("sort", sort);
        paramMap.put("order", order);
        PageHelper.offsetPage(offset, limit);
        List<POperationlog> list = mDao.selectList(mapper, "query", paramMap);
        PageInfo pageInfo = new PageInfo(list);
        JSONObject rtn = new JSONObject();
        JSONArray rows = new JSONArray();
        if (list!=null && list.size()>0) {
            for (POperationlog log : list) {
                JSONObject row = DhxUtil.toFastJsonWithDate(log, lang, timezoneOffset);
                row.put("_ITEM_NO", ++offset);
                rows.add(row);
            }
        }
        rtn.put("rows", rows);
        rtn.put("total", pageInfo.getTotal());
        return rtn;
    }
}
